# -*- encoding: utf-8 -*-
#!/usr/bin/env python
'''
@File    : convert_data.py
@Time    : 2021/02/02 09:25:25
@Author  : Parker
@Version : 1.0
@Contact : 1251633579@qq.com
@Desc    : 
将数据转化为适配的模型输入
'''
#%%
# Lib Config: If custom packages will be used, Please add the following!
# import os, sys
# BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# sys.path.append(BASE_DIR)
import pandas as pd
import numpy as np 
#%%
# 构造label 列表
df = pd.read_csv('./labels.csv')
df.to_csv('vocabulary_label.txt', index=False)
# %%
df = pd.read_csv('./labels.csv', header=None)
# %%
label2id = {v:i for i, v in enumerate(df[0].tolist())}
print(label2id)
# %%


# %%
def transform_multilabel_to_multihot(sample_list, labels_len=215):
    result = np.zeros(labels_len, dtype='i')
    result[sample_list] = 1
    res = result.tolist()
    return res
transform_multilabel_to_multihot([0])
# %%
def convert_data(input_path, out_path):
    test = pd.read_csv(input_path)
    test
    data_list = []
    for tup  in test.itertuples():
        print(tup[1], tup[2])
        label_index = [label2id.get(i) for i in tup[2].split(',')]
        multi_label = transform_multilabel_to_multihot(label_index)
        multi_label.insert(0, tup[1])
        data_list.append(tuple(multi_label))
    labels = ['content'] + list(label2id.keys())
    df = pd.DataFrame.from_records(data_list, columns=labels)
    df.to_csv(out_path, index=False, encoding='utf-8')
# %%
convert_data('./train_152_multilabels.csv', 'train_onehot.csv')
convert_data('./test_152_multilabels.csv', 'test_onehot.csv')
# %%

# read test
df = pd.read_csv('./test_onehot.csv', encoding='utf-8')
df
# %%
